/**************************************************
* TABLE A5: Promotion Analysis (Senate) Full Results
***************************************************/
clear
use masterdata_senate1.dta
sort core_person_id
merge core_person_id using firstcong.dta
keep if _merge == 3
drop _merge
la var firstcong "congress staffer appeared first time in the data"
keep if firstcong >=108
gen exp = 2*(congress - firstcong +1) 
replace totworkdays = ln(totworkdays +1)

gen femalestaff = 0
replace femalestaff = 1 if gender =="F"
gen memfemale = 0
replace memfemale = 1 if minwomen ==1
replace memfemale = 1 if majwomen ==1

by core_person_id member_office_id congress rank, sort: gen tt = 1 if _n == 1 
by member_office_id congress rank, sort: egen nstaffrank = sum(tt)
by member_office_id congress rank, sort: egen nstaffrank_f = sum(tt) if femalestaff == 1
sort member_office_id congress rank nstaffrank_f 
by member_office_id congress rank, sort: replace nstaffrank_f = nstaffrank_f[_n-1] if missing(nstaffrank_f) & _n >=1
replace nstaffrank_f = 0 if nstaffrank_f ==.

forvalues i = 1/7 {
by member_office_id congress, sort: egen numrank`i' = sum(tt) if rank ==`i' & femalestaff ==1
sort member_office_id congress numrank`i'
by member_office_id congress, sort: replace numrank`i' = numrank`i'[_n-1] if missing(numrank`i') & _n >=1
replace numrank`i' = 0 if numrank`i' ==.
}

forvalues i = 1/7 {
by member_office_id congress, sort: egen numrank`i'all = sum(tt) if rank ==`i' 
sort member_office_id congress numrank`i'all
by member_office_id congress, sort: replace numrank`i'all = numrank`i'all[_n-1] if missing(numrank`i'all) & _n >=1
replace numrank`i'all = 0 if numrank`i'all ==.
}

gen femalepeershare = nstaffrank_f/nstaffrank

gen boss = 0
replace boss = numrank1all if rank == 2
replace boss = numrank1all + numrank2all if rank == 3
replace boss = numrank1all + numrank2all + numrank3all if rank ==4
replace boss = numrank1all + numrank2all + numrank3all + numrank4all if rank == 5
replace boss = numrank1all + numrank2all + numrank3all + numrank4all + numrank5all if rank == 6
replace boss = numrank1all + numrank2all + numrank3all + numrank4all + numrank5all + numrank6all if rank == 7

gen bossabove = 0
replace bossabove = numrank1all if rank == 2
replace bossabove = numrank2all if rank == 3
replace bossabove = numrank3all if rank == 4
replace bossabove = numrank4all if rank == 5
replace bossabove = numrank5all if rank == 6
replace bossabove = numrank6all if rank == 7

gen femaleboss = 0
replace femaleboss = numrank1 if rank == 2
replace femaleboss = numrank1 + numrank2 if rank == 3
replace femaleboss = numrank1 + numrank2 + numrank3 if rank ==4
replace femaleboss = numrank1 + numrank2 + numrank3 + numrank4 if rank == 5
replace femaleboss = numrank1 + numrank2 + numrank3 + numrank4 + numrank5 if rank == 6
replace femaleboss = numrank1 + numrank2 + numrank3 + numrank4 + numrank5 + numrank6 if rank == 7

gen femalebossabove = 0
replace femalebossabove = numrank1 if rank == 2
replace femalebossabove = numrank2 if rank == 3
replace femalebossabove = numrank3 if rank == 4
replace femalebossabove = numrank4 if rank == 5
replace femalebossabove = numrank5 if rank == 6
replace femalebossabove = numrank6 if rank == 7

gen femalebossratio = femaleboss/boss
replace femalebossratio = 0 if femaleboss ==.
gen femalebossaboveratio = femalebossabove/bossabove
replace femalebossaboveratio = 0 if femalebossaboveratio ==.

macro define member_char1 "dem memfemale majority chair subchr seniority maj_leader min_leader power up_for_reelection freshman left"
macro define member_char2 "majority chair subchr seniority maj_leader min_leader power up_for_reelection freshman left"
macro define staff_char1 "rank totworkdays nondc notfulltime exp"
macro define staff_char2 "rank totworkdays nondc notfulltime exp grad_degree jdphd"

sort core_person_id member_office_id congress
by core_person_id, sort: gen lagrank = rank[_n-1]
by core_person_id, sort: gen leadrank = rank[_n+1]

gen promotion = .
replace promotion = -(leadrank - rank)

gen int1 = femalestaff*dem
gen int2 = femalepeershare*femalestaff
gen int3 = femalebossratio*femalestaff
macro define interaction "femalepeershare femalebossratio int2 int3"

eststo clear
eststo: quietly reg  promotion femalestaff $member_char1 $staff_char1 i.congress, vce(cluster member_office_id)
eststo: quietly reg  promotion femalestaff $member_char1 $staff_char1 int1 i.congress, vce(cluster member_office_id)
eststo: quietly reg  promotion femalestaff $member_char1 $staff_char2 int1 i.congress, vce(cluster member_office_id)
eststo: quietly areg promotion femalestaff $member_char2 $staff_char1 int1 i.congress, a(member_office_id) vce(cluster member_office_id)
eststo: quietly areg promotion femalestaff $member_char2 $staff_char1 $interaction i.congress, a(member_office_id) vce(cluster member_office_id)
esttab, star(* 0.05 ** 0.01 *** 0.001) drop(*congress*) se ar2
